<script src="<?=base_url()?>templates/js/tabui.js" type="text/javascript"></script>
<script src="<?=base_url()?>templates/js/price_format.js" type="text/javascript"></script>
<script src="<?=base_url()?>templates/js/datetimepicker_css.js" type="text/javascript"></script>
<script type="text/javascript" src="<?=base_url()?>templates/js/jquery.formatCurrencyVN.js"></script>
<script src="<?=base_url()?>templates/js/price.js" type="text/javascript"></script>
<script>    
jQuery(document).ready(function(){
	themanh();
	// load thuoc tinh san pham
	get_product_type();
});
function get_product_type()
{
	var filename = $("#kind").val() ;
	var ProductID = $("#ProductID").val() ;
	
	if(filename=='') return ;
	if(ProductID =='undefined') ProductID = 0 ;
	
	$("#producttype").html('<image src="'+url+'templates/images/loading.gif">');
    $.post(url+"ajax/loadformtype/" + filename + "/" + ProductID ,{},function(data)
    {
        $("#producttype").html(data);                                             
    }); 
}

</script>
<?=form_open('product/addproduct/',array('id'=>'adminformproduct'));?>

<input type="hidden" value="detailed" id="selected_section" name="selected_section">
<div class="tabs cm-j-tabs cm-track">
   <ul>
      <li class="cm-js cm-active" id="detailed"><a>Tổng thể</a></li>
      <li class="cm-js" id="function"><a>Mô tả</a></li>
      <li class="cm-js" id="images"><a>Hình ảnh</a></li>
      <li class="cm-js" id="vanchuyen"><a>Vận chuyển</a></li>
      <li class="cm-js" id="categories"><a>Tặng phẩm</a></li>
      <li class="cm-js" id="qty_discounts"><a>Giảm giá</a></li>
      <!--<li class="cm-js" id="promotion"><a>Mã khuyến mãi</a></li>-->
   </ul>
</div>
<div class="cm-tabs-content">
   <div id="content_detailed" class="hidden">
      <h2 class="subheader">Thông tin</h2>
      <table width="100%" class="form">
         <tr>
            <td width="260" class="required label">Mã SP</td>
            <td colspan="3"><input type="text" id="ProductCode" class="w150" name="ProductCode" value=""></td>
         </tr>
         <tr>
            <td class="required label">Tên sản phẩm</td>
            <td colspan="3"><input type="text" id="ProductName" class="max" name="ProductName" value=""></td>
         </tr>
         <tr>
            <td class="required label">Giá M. Nam</td>
            <td width="294"><input type="text" class="w150" id="Price" name="Price" value=""  onblur="FPrice.set_priceHCM();">
               <span id="Price_label"></span>
               <input name="VAT" type="checkbox" id="VAT" value="1" checked="checked" />
               VAT</td>
            <td width="407">Giá khuyến mãi :
               <input type="text" class="w150" id="PriceCty" name="PriceCty" value=""  onblur="FPrice.set_discountHCM()" /></td>
            <td width="377">Giảm giá :
               <input type="text" class="w150" id="Discount" name="Discount" value="" /></td>
         </tr>
         <tr>
            <td class="required label">Giá M. Bắc</td>
            <td><input type="text" class="w150" id="PriceHN" name="PriceHN" value=""  onblur="FPrice.set_priceHN();" />
               <input name="VAT2" type="checkbox" id="VAT2" value="1" checked="checked" />
               VAT</td>
            <td>Giá khuyến mãi :
               <input type="text" class="w150" id="PriceHNPromo" name="PriceHNPromo" value=""  onblur="FPrice.set_discountHN()" /></td>
            <td>Giảm giá :
               <input type="text" class="w150" id="DiscountHN" name="DiscountHN" value="" /></td>
         </tr>
         <tr>
            <td class="required label">Nhà sản xuất</td>
            <td><select name="MenufactureID">
            		<option value="0">== không có ==</option>
                  <? foreach($menufacture as $row):?>
                  <option value="<?=$row->MenufactureID?>">
                  <?=$row->MenufactureName?>
                  </option>
                  <? endforeach;?>
               </select></td>
            <td colspan="2"><label id="lbdate" style="color:#F00; font-weight:bold;"></label></td>
         </tr>
         <tr>
            <td class="required label">Danh mục sản phẩm</td>
            <td colspan="3"><select name="CategoryID">
                  <? foreach($listcat as $cat):
                            $listsub1 = $this->product->check_parentid($cat->CategoryID);
                            ?>
                  <option value="<?=$cat->CategoryID?>">
                  <?=$cat->CategoryName?>
                  </option>
                  <? if(count($listsub1) > 0){?>
                  <? foreach($listsub1 as $sub1):
                            $listsub2 = $this->product->check_parentid($sub1->CategoryID);
                            ?>
                  <option value="<?=$sub1->CategoryID?>">¦&nbsp;&nbsp;
                  <?=$cat->CategoryName?>
                  &nbsp;¦&nbsp;
                  <?=$sub1->CategoryName?>
                  </option>
                  <? if(count($listsub2) > 0){?>
                  <? foreach($listsub2 as $sub2):?>
                  <option value="<?=$sub2->CategoryID?>">¦&nbsp;&nbsp;
                  <?=$cat->CategoryName?>
                  &nbsp;¦&nbsp;
                  <?=$sub1->CategoryName?>
                  ¦&nbsp;
                  <?=$sub2->CategoryName?>
                  </option>
                  <?  //start level 3
								$listsub3 = $this->product->check_parentid($sub2->CategoryID);
								if(count($listsub3) > 0){
										foreach($listsub3 as $sub3):?>
											<option value="<?=$sub3->CategoryID?>">¦&nbsp;&nbsp;
											<?=$cat->CategoryName?>
											&nbsp;¦&nbsp;
											<?=$sub1->CategoryName?>
											¦&nbsp;
											<?=$sub2->CategoryName?>
											¦&nbsp;
											<?=$sub3->CategoryName?>
											</option>
									<? endforeach;?>
								<? } //end level 3 ?>
                  <? endforeach;?>
                  <? }?>
                  <? endforeach?>
                  <? }?>
                  <? endforeach;?>
               </select></td>
         </tr>
         <tr>
            <td class="label">Tính năng</td>
            <td colspan="3"><textarea id="wysiwyg-advanced" name="ProductFunc"></textarea></td>
         </tr>
         <tr>
            <td class="required label">Khuyến mãi</td>
            <td colspan="3"><input type="checkbox" name="IsHome" value="1"></td>
         </tr>
         <tr>
            <td class="required label">Phạm vi</td>
            <td colspan="3"><input type="checkbox" name="IsHCM" id="IsHCM" value="1" checked="checked" />
               Hồ Chí Minh
               &nbsp;&nbsp;
               <input type="checkbox" name="IsHN" id="IsHN" value="1" checked="checked" />
               Hà Nội</td>
         </tr>
         <tr>
            <td class="required label">Hiển thị</td>
            <td colspan="3"><input type="checkbox" name="IsActive" value="1" checked="checked"></td>
         </tr>
      </table>
      <h2 class="subheader">Hiện trạng sản phẩm</h2>
      <table width="100%" class="form">
         <tr>
            <td width="182" class="required label">Tình trạng</td>
            <td><input type="radio" checked="checked" value="1" id="ProductType" name="ProductType">
               Mới
               <input type="radio" value="2" id="ProductType" name="ProductType">
               Cũ </td>
         </tr>
         <tr>
            <td class="required label">Hàng mới</td>
            <td><input type="text" class="w100" value="100" id="NewPercent" name="NewPercent">
               %</td>
         </tr>
         <tr>
            <td class="required label">Bảo hành</td>
            <td><input type="text" class="w100" name="ProductWarranty" value="12">
               tháng</td>
         </tr>
         <tr>
            <td class="required label">Kích thước</td>
            <td><input type="text" class="w100" name="ProductSize" value="">
               ví dụ: 20x20</td>
         </tr>
         <tr>
            <td class="required label">Thể tích</td>
            <td><input type="text" class="w100" name="ProductCapacity" value="">
               LÍT</td>
         </tr>
         <tr>
            <td class="required label">Trọng lượng</td>
            <td><input type="text" class="w100" name="ProductWeight" value="">
               KG</td>
         </tr>
         <tr>
            <td class="required label">Hiện trạng kho HCM</td>
            <td><input type="radio" checked="checked" value="1" id="ProductStatus" name="ProductStatus">
               Còn hàng
               <input type="radio" value="2" id="ProductStatus" name="ProductStatus">
               Hết hàng
               <input type="radio" value="3" id="ProductStatus" name="ProductStatus">
               Có thể đặt hàng<br />
               <input type="radio" value="4" id="ProductStatus" name="ProductStatus">
               Tùy chọn
               <input type="text" name="ProductStatusText" value="" class="w400"></td>
         </tr>
         <tr>
            <td class="required label">Hiện trạng kho HN</td>
            <td><input type="radio" checked="checked" value="1" id="ProductStatusHN" name="ProductStatusHN">
               Còn hàng
               <input type="radio" value="2" id="ProductStatusHN" name="ProductStatusHN">
               Hết hàng
               <input type="radio" value="3" id="ProductStatusHN" name="ProductStatusHN">
               Có thể đặt hàng<br />
               <input type="radio" value="4" id="ProductStatusHN" name="ProductStatusHN">
               Tùy chọn
               <input type="text" name="ProductStatusTextHN" id="ProductStatusTextHN" value="" class="w400"></td>
         </tr>
      </table>
   </div>
   <div id="content_function" class="hidden">
      <h2 class="subheader">Mô tả</h2>
      <table class="form">
         <tr>
            <td class="label">Mô tả</td>
            <td><textarea id="wysiwyg-simple" name="Description"></textarea></td>
         </tr>
         <tr>
            <td class="required label">Loại sản phẩm</td>
            <td><select id="kind" name="kind" onchange="get_product_type()">
                  <option value=""> --- select --- </option>
                  <?foreach($dt as $type):?>
                  <option value="<?=$type->template?>">
                  <?=$type->type_name?>
                  </option>
                  <?endforeach;?>
               </select></td>
         </tr>
         <!--<script language="javascript">
                var loca = document.getElementById("kind").options ;
                for(var i=0;i<loca.length;i++)
                {
                    if(loca[i].value == "<?//=$rs->ProductKind?>")
                        loca[i].selected = true ;
                }
            </script>-->
      </table>
      <h2 class="subheader">Tính năng sản phẩm</h2>
      <div id="producttype"></div>
   </div>
   <div id="content_images" class="hidden">
      <h2 class="subheader">Hình ảnh</h2>
      <table width="100%" class="form">
         <tr>
            <td class="label">Hình chính</td>
            <td><div id="main_img"><img src="<?=base_url_site()?>uploads/no_image.gif" alt=""></div>
               <input type="hidden" name="ProductImg" value="" id="ProductImg"></td>
         </tr>
         <tr>
            <td class="label">Hình ảnh</td>
            <td><div id="upload_button" class="  "><span>Chọn File Upload<span></span></span></div>
               <div id="status_message">Click chọn hình để đưa vào hình
                  chỉnh của sản phẩm</div>
               <ul id="list_img">
                  <? foreach($listimg as $val):?>
                  <li class="success">
                     <div  align="center"><img width="85" height="85" src="<?=base_url_site()?>uploads/images/<?=$val->ImagePath?>" onclick="chosen(<?=$val->ImageID?>)"> <a class="delete_button" id="<?=$val->ImageID?>" href="javascript:;"> Xóa</a><span style="display: none;" id="v<?=$val->ImageID?>">
                        <?=$val->ImagePath?>
                        </span></div>
                  </li>
                  <? endforeach;?>
               </ul></td>
         </tr>
      </table>
   </div>
   <div id="content_vanchuyen" class="hidden">
      <h2 class="subheader">Vận chuyển</h2>
      <table width="100%" class="form">
         <tr>
            <td class="required label">Vận chuyển</td>
            <td><select name="IsAllowShipping">
                  <option value="0">Không chấp nhận chuyển hàng</option>
                  <option value="1">Chấp nhận chuyển hàng</option>
               </select></td>
         </tr>
         <tr>
            <td class="required label">Miễn phí vận chuyển</td>
            <td><input type="checkbox" name="ShippingFree" value="1"></td>
         </tr>
         <tr>
            <td class="label">Trọng lượng</td>
            <td><input type="text" name="ShippingWeight" value="">
               KG </td>
         </tr>
         <tr>
            <td class="label">Giá/ trọng lượng</td>
            <td><input type="text" name="ShippingPrice" value="">
               VND </td>
         </tr>
      </table>
   </div>
   <div id="content_categories" class="hidden">
      <h2 class="subheader">Tặng phẩm</h2>
      <a href="javascript:;" class="add">Thêm</a>
      <div id="list_tangpham">
         <div id="tangpham">
            <input type="hidden" id="total_tangpham" name="GiftsID[]">
            <table width="100%" class="form">
               <tr>
                  <td class="label">Thông tin tặng phẩm</td>
                  <td><input type="text" name="GiftsDes[]" class="w500" value=""></td>
               </tr>
               <tr>
                  <td class="label">Phạm vi áp dụng</td>
                  <td><input type="text" class="w500" id="GiftsArea[]" name="GiftsArea[]" value="" /></td>
               </tr>
               <tr>
                  <td class="label">Bắt đầu</td>
                  <td><input type="text" class="w150" id="GiftsStart" name="GiftsStart[]" value="">
                     <a href="javascript:;" onclick="javascript:NewCssCal ('GiftsStart','yyyyMMdd')"><img src="<?=base_url()?>templates/images/date_picker/cal.gif" alt=""></a></td>
               </tr>
               <tr>
                  <td class="label">Kết thúc</td>
                  <td><input type="text" class="w150" id="GiftsEnd" name="GiftsEnd[]" value="">
                     <a href="javascript:;" onclick="javascript:NewCssCal ('GiftsEnd','yyyyMMdd')"><img src="<?=base_url()?>templates/images/date_picker/cal.gif" alt=""></a></td>
               </tr>
            </table>
         </div>
      </div>
   </div>
   <script type="text/javascript" >
        $(function() {

            var i = $('input#total_tangpham').size();
            $('a.add').click(function() {
            var d = new Date();
            var j= d.getMilliseconds();                
                var html = '<div id="tangpham"><input type="hidden" id="total_tangpham" name="GiftsID[]">';
                    html += '<table class="form">';
                    html += '<tr>';
                    html += '<td class="label">Thông tin tặng phẩm</td>';
                    html += '<td><input type="text" name="GiftsDes[]" class="w500" value=""></td>';
                    html += '</tr><tr>';
					html += '<td class="label">Phạm vi áp dụng</td>';
                    html += '<td><input type="text" name="GiftsArea[]" class="w500" value=""></td>';
                    html += '</tr><tr>';
                    html += '<td class="label">Bắt đầu</td>';
                    html += '<td><input type="text" class="w150" id="GiftsStart'+j+'" name="GiftsStart[]" value=""> <a href="javascript:;" onclick="javascript:NewCssCal (\'GiftsStart'+j+'\',\'yyyyMMdd\')"><img src="<?=base_url()?>templates/images/date_picker/cal.gif" alt=""></a></td>';
                    html += '</tr><tr>';
                    html += '<td class="label">Kết thúc</td>';
                    html += '<td><input type="text" class="w150" id="GiftsEnd'+j+'" name="GiftsEnd[]" value=""> <a href="javascript:;" onclick="javascript:NewCssCal (\'GiftsEnd'+j+'\',\'yyyyMMdd\')"><img src="<?=base_url()?>templates/images/date_picker/cal.gif" alt=""></a></td>';
                    html += '</tr></table>';
                    html += '<a id="remScnt" href="javascript:;" class="remove">Xóa</a></div></div>';
                $(html).animate({ 
                    opacity: "show" 
                }, "slow").appendTo('#list_tangpham');
                i++;
            });
            $('#remScnt').live('click', function() {
                if( i > 1 ) {
                    $(this).parents('div#tangpham').remove();
                    i--;
                }
                return false;
            });
                     
        });
        </script>
   <div id="content_qty_discounts" class="hidden">
      <h2 class="subheader">Giảm giá</h2>
      <table width="100%" class="form">
         <tr>
            <td width="40%" class="label">Số tiền giảm giá (HCM)</td>
            <td width="60%"><input type="text" class="w150" name="DiscountNumber" id="DiscountNumber" value=""></td>
         </tr>
         <tr>
            <td class="label">Số tiền giảm giá (HN)</td>
            <td><input type="text" class="w150" name="DiscountNumberHN" id="DiscountNumberHN" value="" /></td>
         </tr>
         <tr>
            <td class="label">Khu vực</td>
            <td><input type="text" class="w150" id="DiscountArea" name="DiscountArea" value="" /></td>
         </tr>
         <tr>
            <td class="label">Bắt đầu</td>
            <td><input type="text" class="w150" id="DiscountStartDate" name="DiscountStartDate" value=""></td>
         </tr>
         <tr>
            <td class="label">Kết thúc</td>
            <td><input type="text" class="w150" id="DiscountEndDate" name="DiscountEndDate" value=""></td>
         </tr>
      </table>
   </div>
</div>
<input type="submit" name="bt_submit" value="" class="themmoi">
<?=form_close();?>
<script type="text/javascript">
     $("#adminform").validate({
        rules: {
            ProductName: "required",
            Alias: "required",
        },
        messages: {
            ProductName: "Vui lòng điền tên sản phẩm",
            Alias: "Mã trên link không được để trống.",
        } 
    });
    
</script> 
<script>
    $(function() {
        var dates = $( "#DiscountStartDate, #DiscountEndDate" ).datepicker({
            changeMonth: true,
            dateFormat: 'yy-mm-dd', 
            numberOfMonths: 1,
            onSelect: function( selectedDate ) {
                var option = this.id == "DiscountStartDate" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" );
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
    });

</script> 
<script type="text/javascript" >
    $(function(){
        var btnUpload=$('#upload_button');
        var status=$('#status_message');
        new AjaxUpload(btnUpload, {
            action: '<?=base_url()?>ajax/uploader/',
            name: 'uploadfile',
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|gif|)$/.test(ext))){ 
                    // extension is not allowed 
                    status.text('File cho phép Upload JPG, PNG, JPEG, GIF');
                    return false;
                }
                status.text('Đang tải File lên. Vui lòng đợi');
            },
            onComplete: function(file, response){ 
                
                //On completion clear the status
                status.text('');
                //Add uploaded file to list
                if(response!="error"){
                var str = response;
                var exploded = str.split(',');
                    $('<li></li>').appendTo('#list_img').html('<div  align="center"><img  onclick="chosen('+exploded[0]+')" src="'+urlsite+'uploads/images/'+exploded[1]+'" width="85" height="85"><a href="javascript:;" id="'+exploded[0]+'" class="delete_button"> Xóa</a><span id="v'+exploded[0]+'" style="display: none;">'+exploded[1]+'</span></div>').addClass('success');
                } else{
                    $('<li></li>').appendTo('#list_img').text(file).addClass('error');
                }
            }
        });
        
    });
</script> 
<script type="text/javascript">
	$(function() {
	$(".delete_button").click(function() {
	if(confirm("Bạn có chắc chắn muốn xóa hình ảnh này\nĐồng ý chọn: OK\nHủy chọn: Cancel")){
	var id = $(this).attr("id");
	var dataString = 'id='+ id ;
	var parent = $(this).parent();
		
	$.ajax({
	   type: "POST",
	   url: url+"product/delimg",
	   data: dataString,
	   cache: false,
	   beforeSend: function(){
		parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "past");;
	   }, 
	   success: function(){
		//parent.slideUp('slow', function() {$(this).remove();});
	  }   
	 });
	 }
	return false;
		});
	});
	function chosen(id)
	{
		if(id)
		{
			var vl = $('#v'+id).text();
			$('#ProductImg').val(vl);
			$('#main_img').html('<img src="' + urlimg + vl + '" width="85" height="85" />');
		}
		else
		{
			return false;
		}
	}     

</script> 
